<?php

declare(strict_types=1);

namespace App\Http\Controllers\Manage;

use App\Repositories\Manage\DictRepository;

class DictController extends ManageBaseController
{

    private $dictRepo = NULL;

    public function __construct(DictRepository $dictRepo)
    {
        parent::__construct();
        $this->dictRepo = $dictRepo;
    }

    public function index()
    {
        $filter = request()->all();
        $menus = $this->dictRepo->getList($filter, $this->pageNum, $this->pageSize);
        return retSuc($menus);
    }

    public function options($typeCode = null)
    {

    }

    public function update($id = null)
    {
        $data = request()->all();
        $last_id = $this->dictRepo->saveOrUpdate($data, $id ?? 0);
        if (!$last_id) {
            return retFail("更新失败");
        }

        $menu = $this->dictRepo->getOne($last_id);

        return retSuc($menu);
    }

    public function detail($id)
    {
        $id = intval($id);
        if (!$id)
            return retFail("参数错误");

        $res = $this->dictRepo->getOne($id);

        return retSuc($res);
    }

    public function delete($id)
    {
        $ids = array_filter(array_unique(explode(",", $id)));
        if (empty($ids)) {
            return retFail("参数错误");
        }

        return $this->dictRepo->batchDelete($ids);
    }

}
